//
//  MOPersonSetViewCell.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/2.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOPersonSetViewCell.h"

@interface MOPersonSetViewCell ()

@property(nonatomic,strong)UILabel *nameLabel;

@property(nonatomic,strong)UISwitch *pSwitch;

@property (nonatomic, strong) UIImageView *arrowImageView;
@end

@implementation MOPersonSetViewCell

+(instancetype)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
    NSString *ID = @"MOPersonSetViewCell";
    MOPersonSetViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil){
        cell = [[MOPersonSetViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell creatView];
    }
    return cell;
}


-(void)creatView
{
    [self addSubview:self.nameLabel];
    [self addSubview:self.pSwitch];
    [self addSubview:self.arrowImageView];
}

-(void)setModel:(MOPersonSetModel *)model
{
    if ([model.type integerValue]==1) {
        self.pSwitch.hidden = NO;
        self.arrowImageView.hidden = YES;
        self.nameLabel.attributedText = [self contentString:model.nameStr];
    }else{
        self.pSwitch.hidden = YES;
        self.arrowImageView.hidden = NO;
        self.nameLabel.attributedText = [self contentString:model.nameStr];
    }
    self.pSwitch.on = [model.isOpen integerValue]==1?YES:NO;
}

-(NSMutableAttributedString *)contentString:(NSString *)str
{
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    NSMutableAttributedString *setString = [[NSMutableAttributedString alloc] initWithString:str];
    
    NSRange range;
    if ([str rangeOfString:@"开启"].location!=NSNotFound) {
        range = NSMakeRange([str rangeOfString:@"开启"].location, [str rangeOfString:@"开启"].length);
    }else if ([str rangeOfString:@"关闭"].location!=NSNotFound){
        range = NSMakeRange([str rangeOfString:@"关闭"].location, [str rangeOfString:@"关闭"].length);
    }else{
        range = NSMakeRange(0, 0);
    }
    [setString  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    [setString addAttribute:NSForegroundColorAttributeName value:KBlueColor_2C94FF range:range];
    
    return setString;
}

-(void)switchAction:(UISwitch *)pSwitch
{
    NSLog(@"%d",pSwitch.on);
    if (self.clickSwitchBlock) {
        self.clickSwitchBlock([NSString stringWithFormat:@"%d",pSwitch.on]);
    }
}

#pragma mark -- 懒加载
-(UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, kHConversions(54))];
        _nameLabel.font = FONT_SIZE(15);
        _nameLabel.textColor = KBlackColor_2A2A2A;
    }
    return _nameLabel;
}

-(UISwitch *)pSwitch
{
    if (!_pSwitch) {
        _pSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-15-45, 0, 0, 0)];
        _pSwitch.centerY = kHConversions(54)/2;
        [_pSwitch setTintColor:KGrayColor_CDD2D8];
        [_pSwitch setOnTintColor:KBlueColor_2C94FF];
        [_pSwitch setThumbTintColor:[UIColor whiteColor]];
        _pSwitch.backgroundColor = KGrayColor_CDD2D8;
        _pSwitch.layer.cornerRadius = 15.5f;
        _pSwitch.layer.masksToBounds = YES;
        [_pSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _pSwitch;
}

- (UIImageView *)arrowImageView{
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 17 - 7, 0, 7, 12)];
        _arrowImageView.centerY = kHConversions(54)/2;
        _arrowImageView.image = IMAGE(@"setUp_arrow");
    }
    return _arrowImageView;
}


@end
